﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;

namespace AricentTestProject
{
    public partial class Registration : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CurrentUser != null)
            {
                Response.Redirect("/");
            }
            ErrLabel.Text = "";
        }

        protected void onRegister(object sender, EventArgs e)
        {
            var user = UserExtensions.getByLoginPassword(Login.Text, Password.Text);
            if (user != null)
            {
                ErrLabel.Text = "User is already exists";
                return;
            }

            user = new UserInfo();
            user.Id = Guid.NewGuid().ToString();
            user.Login = Login.Text;
            user.Password = Password.Text;
            user.save();

            var profile = new UserProfileInfo();
            profile.Id = user.Id;
            profile.FirstName = FirstName.Text;
            profile.LastName = LastName.Text;
            profile.save();

            CurrentUser = user;
            Response.Redirect("/");
        }
    }
}
